package cn.ljy.uem.web.action.frontend.mobile.student;

import cn.ljy.authority.config.security.TokenUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.model.param.UserPasswordParam;
import cn.ljy.authority.service.SysUserService;
import cn.ljy.authority.service.SysUserTokenFrontendService;
import cn.ljy.authority.web.controller.backstage.BaseController;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.uem.model.SchoolStudent;
import cn.ljy.uem.service.SchoolStudentService;
import cn.ljy.uem.service.common.CommonSchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "移动端学生模块")
@RestController
@RequestMapping(value = "/api/mobile/schoolstudent")
public class SchoolStudentMobileController extends BaseController {

    @Autowired
    private SchoolStudentService schoolStudentService;

    @Autowired
    private CommonSchoolService commonSchoolService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserTokenFrontendService sysUserTokenFrontendService;
//    @ApiOperation(value = "测试需要学生登录才能访问的接口，后期应该删除")
//    @GetMapping("/test")
//    public ResponseData test(){
//        return ResponseData.success("测试");
//    }

    @ApiOperation(value = "获取当前登录学生账户信息")
    @GetMapping("/session")
    public ResponseData session(){
        String token= TokenUtil.getAppToken();//读取前台token的值
        //获取登录账户信息
        SysUser user=sysUserTokenFrontendService.getUserFromToken(token);
        //获取登录账户对应的学生信息
        SchoolStudent schoolStudent=commonSchoolService.getStudent(user);
        return ResponseData.success(schoolStudent);
    }

    @ApiOperation(value = "修改当前账户的密码")
    @PatchMapping("/session/password")
    public ResponseData updatePassword(@Valid @RequestBody UserPasswordParam userPasswordParam){
        String token= TokenUtil.getAppToken();//读取前台token的值
        //获取登录账户信息
        SysUser user=sysUserTokenFrontendService.getUserFromToken(token);
        //获取登录账户对应的学生信息
//        SchoolStudent schoolStudent=commonSchoolService.getStudent(user);
        if(user!=null){
            userService.updatePassword(user.getUsername(),userPasswordParam.getPassword(), userPasswordParam.getNewPassword(), userPasswordParam.getConfirmPassword());
        }
        return ResponseData.success(null);
    }

/*    @ApiOperation(value = "学生登录")
    @GetMapping("/login")
    public ResponseData login(String username,String password){
        String token=schoolStudentService.loginAndGetToken(username,password);
        if(StringUtils.hasLength(token)){
            return ResponseData.success(token);
        }else{
            return ResponseData.error("登录失败");
        }
    }

    @ApiOperation(value = "学生注销")
    @GetMapping("/logout")
    public ResponseData logout(HttpServletRequest request){
        //读取token的值
        String token= SchoolTokenUtil.getSchoolStudentToken(request);
        //移除tokeen
        schoolStudentService.removeToken(token);
        return ResponseData.success("注销成功");
    }*/
}
